//
//  XRmenuPublic.m
//  XRmenu
//
//  Created by Gao Yan on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRmenuPublic.h"
#import "XRmenuFileManage.h"
#import "XRmenuError.h"

#import "JSON.h"

#define kRootPathName @"XRmenu"

@implementation XRmenuPublic
#pragma mark 通用日期类函数
// 获取指定格式的日期字符串
+ (NSString *) getChineseDate{
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	//設定日期格式
	[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]];
	[dateFormatter setDateFormat:@"yyyy年MM月dd日 EEEE"];
	//設定日期字串
	NSString *formattedDateString = [dateFormatter stringFromDate:[NSDate date]];
	return formattedDateString;
}

+ (NSString *) getChineseTime:(NSDate *)cDate{
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	//設定日期格式
	[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]];
	[dateFormatter setDateFormat:@"HH:mm:SS"];
	//設定日期字串
	NSString *formattedDateString = [dateFormatter stringFromDate:cDate];
	return formattedDateString;
}

+ (NSString *) getChineseTimeStyle1:(NSDate *)cDate{
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	//設定日期格式
	[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]];
	[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
	//設定日期字串
	NSString *formattedDateString = [dateFormatter stringFromDate:cDate];
	return formattedDateString;
}

+ (NSString *) getChineseTimeStyle2:(NSDate *)cDate{
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	//設定日期格式
	[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease]];
	[dateFormatter setDateFormat:@"yyMMddHHmmss"];
	//設定日期字串
	NSString *formattedDateString = [dateFormatter stringFromDate:cDate];
	return formattedDateString;
}

#pragma mark 语言翻译
+ (NSString*) translateString:(NSString*)txt
{
//	DebugLog(@"%@", [APPSESSION.translate getStringValueAtPath:[NSString stringWithFormat:@"%@/%@", txt, APPSESSION.language]]);
	NSString *tmp = [APPSESSION.translate getStringValueAtPath:[NSString stringWithFormat:@"%@/%@", txt, APPSESSION.language]];
	if ([tmp isEqualToString:[NSString stringWithFormat:@"%@/%@", txt, APPSESSION.language]]) {
		return txt;
	}
	return tmp;
}
+ (NSString*) translateImageName:(NSString*)filename
{
	// filename包含/,无法使用nsdict的扩展方法
	NSDictionary *tr = [APPSESSION.translate objectForKey:filename];
	NSString *imgname;	
	if (tr) {
		imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
	}else {
		imgname = [XRmenuPublic getGeneralSkinImg:filename];
	}
	return imgname;
}
#pragma mark 依据plist生成对象
+ (UIButton *) generalButton:(NSString*)stylename ButtonLable:(NSDictionary*)ldict ButtonFrame:(CGRect)fr;
{
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"UIButton CSS"];
	
	if ([tdict objectForKey:stylename]){
		UIButton *bt = [[[UIButton alloc] initWithFrame:fr] autorelease];
		NSDictionary *btdict = [tdict objectForKey:stylename];

		BOOL yn;
		if([btdict objectForKey:@"button is image"]){
			yn = [(NSNumber*)[btdict objectForKey:@"button is image"] boolValue];
		}else {
			yn = NO;
		}
		if (yn) {
			// 图形背景按钮,背景图片不需要翻译
			if ([[btdict objectForKey:@"button images"] objectForKey:@"Normal"]) {
				[bt setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:[[btdict objectForKey:@"button images"] objectForKey:@"Normal"]]] forState:UIControlStateNormal];
			}
			if ([[btdict objectForKey:@"button images"] objectForKey:@"Highlighted"]) {
				[bt setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:[[btdict objectForKey:@"button images"] objectForKey:@"Highlighted"]]] forState:UIControlStateHighlighted];
			}
			if ([[btdict objectForKey:@"button images"] objectForKey:@"Disabled"]) {
				[bt setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:[[btdict objectForKey:@"button images"] objectForKey:@"Disabled"]]] forState:UIControlStateDisabled];
			}
			if ([[btdict objectForKey:@"button images"] objectForKey:@"Selected"]) {
				[bt setBackgroundImage:[UIImage imageWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:[[btdict objectForKey:@"button images"] objectForKey:@"Selected"]]] forState:UIControlStateSelected];
			}
		}else {
			// 色块按钮
			if ([[btdict objectForKey:@"button colors"] objectForKey:@"Background"]) {
				[bt setBackgroundColor:[XRmenuPublic generalColor:[[btdict objectForKey:@"button colors"] objectForKey:@"Background"]]];
			}
		}
		
		BOOL isimg = NO;
		if ([ldict objectForKey:@"lable is image"]) {
			isimg = [(NSNumber*)[ldict objectForKey:@"lable is image"] boolValue];
		}else {
			isimg = NO;
		}
		if (isimg) {
			bt = [self adjustButtonsImage:bt Parms:ldict];
		}else {
			if ([[btdict objectForKey:@"button colors"] objectForKey:@"Normal"]) {
				[bt setTitleColor:[XRmenuPublic generalColor:[[btdict objectForKey:@"button colors"] objectForKey:@"Normal"]] forState:UIControlStateNormal];
			}
			if ([[btdict objectForKey:@"button colors"] objectForKey:@"Highlighted"]) {
				[bt setTitleColor:[XRmenuPublic generalColor:[[btdict objectForKey:@"button colors"] objectForKey:@"Highlighted"]] forState:UIControlStateHighlighted];
			}
			if ([[btdict objectForKey:@"button colors"] objectForKey:@"Disabled"]) {
				[bt setTitleColor:[XRmenuPublic generalColor:[[btdict objectForKey:@"button colors"] objectForKey:@"Disabled"]] forState:UIControlStateDisabled];
			}
			if ([[btdict objectForKey:@"button colors"] objectForKey:@"Selected"]) {
				[bt setTitleColor:[XRmenuPublic generalColor:[[btdict objectForKey:@"button colors"] objectForKey:@"Selected"]] forState:UIControlStateSelected];
			}
			
			if ([ldict objectForKey:@"Normal"]) {
				[bt setTitle:loc([ldict objectForKey:@"Normal"]) forState:UIControlStateNormal];
			}
			if ([ldict objectForKey:@"Highlighted"]) {
				[bt setTitle:loc([ldict objectForKey:@"Highlighted"]) forState:UIControlStateHighlighted];
			}
			if ([ldict objectForKey:@"Disabled"]) {
				[bt setTitle:loc([ldict objectForKey:@"Disabled"]) forState:UIControlStateDisabled];
			}
			if ([ldict objectForKey:@"Selected"]) {
				[bt setTitle:loc([ldict objectForKey:@"Selected"]) forState:UIControlStateSelected];
			}
		}

		return bt;
	}else {
		return [[[UIButton alloc] initWithFrame:fr] autorelease];
	}

}

+ (UIButton*) adjustButtonsImage:(UIButton*)bt Parms:(NSDictionary*)ldict
{
	NSDictionary *tr;
	NSString *imgname;
	
	if ([ldict objectForKey:@"Normal"]) {
		tr = nil; imgname = nil;
		tr = [APPSESSION.translate objectForKey:[ldict objectForKey:@"Normal"]];
		if (tr) {
			imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
		}else {
			imgname = [XRmenuPublic getGeneralSkinImg:[ldict objectForKey:@"Normal"]];
		}
		[bt setImage:[UIImage imageWithContentsOfFile:imgname] forState:UIControlStateNormal];
	}
	if ([ldict objectForKey:@"Highlighted"]) {
		tr = nil; imgname = nil;
		tr = [APPSESSION.translate objectForKey:[ldict objectForKey:@"Highlighted"]];
		if (tr) {
			imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
		}else {
			imgname = [XRmenuPublic getGeneralSkinImg:[ldict objectForKey:@"Highlighted"]];
		}
		[bt setImage:[UIImage imageWithContentsOfFile:imgname] forState:UIControlStateHighlighted];
	}
	if ([ldict objectForKey:@"Disabled"]) {
		tr = nil; imgname = nil;
		tr = [APPSESSION.translate objectForKey:[ldict objectForKey:@"Disabled"]];
		if (tr) {
			imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
		}else {
			imgname = [XRmenuPublic getGeneralSkinImg:[ldict objectForKey:@"Disabled"]];
		}
		[bt setImage:[UIImage imageWithContentsOfFile:imgname] forState:UIControlStateDisabled];
	}
	if ([ldict objectForKey:@"Selected"]) {
		tr = nil; imgname = nil;
		tr = [APPSESSION.translate objectForKey:[ldict objectForKey:@"Selected"]];
		if (tr) {
			imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
		}else {
			imgname = [XRmenuPublic getGeneralSkinImg:[ldict objectForKey:@"Selected"]];
		}
		[bt setImage:[UIImage imageWithContentsOfFile:imgname] forState:UIControlStateSelected];
	}	
	return bt;
}

+(UITextAlignment) getTextAlignment:(NSUInteger)alignmentMode{	// 1:left 2:center 3:right Default:1
	switch (alignmentMode) {
		case 1:
			return UITextAlignmentLeft;
			break;
		case 2:
			return UITextAlignmentCenter;
			break;
		case 3:
			return UITextAlignmentRight;
			break;
		default:
			return UITextAlignmentLeft;
			break;
	}
}

+ (UILabel *) generalLabel:(NSString*)stylename LabelText:(NSString *)text LabelFrame:(CGRect)fr;
{
	
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Lable CSS"];

	if ([tdict objectForKey:stylename]){
		UILabel *lb = [[[UILabel alloc] initWithFrame:fr] autorelease];
		NSDictionary *lbdict = [tdict objectForKey:stylename];

		// 背景
		if ([lbdict objectForKey:@"background"]) {
			NSDictionary *bdict = [lbdict objectForKey:@"background"];
			if ([bdict getBoolValAtPath:@"background is image"]) {
				//
			}else {
				[lb setBackgroundColor:[XRmenuPublic generalColor:[bdict getStringValueAtPath:@"background color"]]];
			}
		}
		//字体
		[lb setMinimumFontSize:8];
		[lb setAdjustsFontSizeToFitWidth:YES];
		if ([lbdict objectForKey:@"font style"]) {
			UIFont *font = [XRmenuPublic generalFont:[lbdict objectForKey:@"font style"]];
			[lb setFont:font];
			[lb setTextColor:[XRmenuPublic generalFontColor:[lbdict objectForKey:@"font style"]]];
		}
		[lb setText:loc(text)];
		// 居中
		if ([lbdict objectForKey:@"alignment"]) {
			[lb setTextAlignment:[self getTextAlignment:[[lbdict objectForKey:@"alignment"] intValue]]];
		}else {
			[lb setTextAlignment:UITextAlignmentLeft];
		}		
		return lb;
	}else {
		return [[[UILabel alloc] initWithFrame:fr] autorelease];
	}
	
}


+ (UIImageView *) generalImageView:(CGRect)fr
{
	UIImageView *imgV=[[[UIImageView alloc] initWithFrame:fr] autorelease];
	return imgV;
}

+ (VideoPlayer *) generalVideoView:(CGRect)fr
{
	VideoPlayer *vp=[[[VideoPlayer alloc] initWithFrame:fr ] autorelease];
	vp.frame=fr;
	return vp;
}

+ (UITextField *) generalTextField:(NSString*)stylename FieldFrame:(CGRect)fr{
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"UITextField CSS"];
	
	if ([tdict objectForKey:stylename]){
		UITextField *ret = [[[UITextField alloc] initWithFrame:fr] autorelease];
		NSDictionary *style = [tdict objectForKey:stylename];
		
		if ([style objectForKey:@"adjustsFontSizeToFitWidth"]){
			ret.adjustsFontSizeToFitWidth = YES;
		}else {
			ret.adjustsFontSizeToFitWidth = NO;
		}
		if ([style objectForKey:@"ispassword"]){
			ret.secureTextEntry = YES;
			ret.clearButtonMode = UITextFieldViewModeWhileEditing;
		}
		
		if ([style objectForKey:@"background"]){
			NSDictionary *bg = [style objectForKey:@"background"];
			BOOL yn;
			if([bg objectForKey:@"background is image"]){
				yn = [(NSNumber*)[bg objectForKey:@"background is image"] boolValue];
			}else {
				yn = NO;
			}
			if (yn) {
				ret.background = [UIImage imageWithContentsOfFile:[self getGeneralImg:[bg objectForKey:@"background image"]]];
			}else {
				UIColor *c;
				if ([bg objectForKey:@"background color"]) {
					c = [self generalColor:[bg objectForKey:@"background color"]];
				}else {
					c = [UIColor clearColor];
				}
				
				ret.backgroundColor = c;
			}

		}
		
		if ([style objectForKey:@"font style"]){
			ret.font = [self generalFont:[style objectForKey:@"font style"]];
			ret.textColor = [self generalFontColor:[style objectForKey:@"font style"]];
		}else {
			ret.font = [self generalFont:@"default font"];
			ret.textColor = [self generalFontColor:@"default font"];
		}
		
		ret.leftView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 3)] autorelease];
		ret.leftViewMode = UITextFieldViewModeAlways;
		ret.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		return ret;
	}else {
		return [[[UITextField alloc] initWithFrame:fr] autorelease];
	}
}

+ (UIFont*) generalFont:(NSString*)stylename{
	NSString *fontName;
	NSInteger fontSize;
	
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Font CSS"];
	if ([tdict objectForKey:stylename]){
		NSDictionary *style = [tdict objectForKey:stylename];
		if ([style objectForKey:@"font name"]) {
			fontName = [NSString stringWithString:[style objectForKey:@"font name"]];
		}else {
			fontName = [NSString stringWithString:@"Arial"];
		}
		if ([style objectForKey:@"font size"]) {
			fontSize = [[style objectForKey:@"font size"] intValue];
		}else {
			fontSize = 14;
		}
		return [UIFont fontWithName:fontName size:fontSize];
		
	}else {
		return [UIFont fontWithName:[[UIFont fontNamesForFamilyName:@"Arial"] objectAtIndex:1] size:14.0f];
	}
}

+ (UIColor*) generalFontColor:(NSString*)stylename
{
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Font CSS"];
	if ([tdict objectForKey:stylename]){
		NSDictionary *style = [tdict objectForKey:stylename];
		if ([style objectForKey:@"font color"]) {
			return [self generalColor:[style objectForKey:@"font color"]];
		}else {
			return [UIColor clearColor];
		}
	}else {
		return [UIColor clearColor];
	}
}

+ (UIColor*) generalColor:(NSString*)colorname
{
	UIColor *c;
	if ([colorname isEqualToString:@"black color"]) {
		c = [UIColor blackColor];
	}else if ([colorname isEqualToString:@"blue color"]) {
		c = [UIColor blueColor];
    }else if ([colorname isEqualToString:@"yellow color"]) {
		c = [UIColor yellowColor];
    }else if ([colorname isEqualToString:@"clear color"]) {
            c = [UIColor clearColor];
	}else if ([colorname isEqualToString:@"white color"]) {
		c = [UIColor whiteColor];
	}else if ([colorname isEqualToString:@"green color"]) {
		c = [UIColor greenColor];
	}else if ([colorname isEqualToString:@"red color"]) {
		c = [UIColor redColor];
	}else if ([colorname isEqualToString:@"gray color"]) {
		c = [UIColor grayColor];
	}else if ([colorname isEqualToString:@"eeeeee"]){
		c= [UIColor colorWithRed:238 green:238 blue:238 alpha:255];
	}else if ([colorname isEqualToString:@"ffce0a"]){
		c= [UIColor colorWithRed:255 green:206 blue:0 alpha:255];
	}else if ([colorname isEqualToString:@"3e3f40"]){
		c= [UIColor colorWithRed:62 green:63 blue:64 alpha:255];
	}else {
		NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"Color CSS"];
		if ([tdict objectForKey:colorname]){
			NSDictionary *style = [tdict objectForKey:colorname];
			c = RGBA([[style objectForKey:@"r"] floatValue], [[style objectForKey:@"g"] floatValue], [[style objectForKey:@"b"] floatValue], [[style objectForKey:@"alpha"] floatValue] / 255.0f);
		}else {
			c = [UIColor clearColor];
		}
	}
	return c;
}

+ (NSString *) getGeneralImg:(NSString *)imgfile
{
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imgfile]];
	if ([[NSFileManager defaultManager] fileExistsAtPath:retResault]) {
		return retResault;
	}else{
		NSString *ret2 = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imgfile]];
		if ([[NSFileManager defaultManager] fileExistsAtPath:ret2]) {
			return ret2;
		}else {            
            NSString *defaultImg=[[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:@"XRmenu/default.png"];
			return defaultImg;//返回默认图片
		}
	}
}

+ (NSString *) getGeneralSkinImg:(NSString *)imgfile
{
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"UISkin/%@/%@",APPSESSION.uistyle, imgfile]];
	if ([XRmenuFileManage fileExistByPath:retResault]) {
		return retResault;
	}else{
		NSString *ret2 = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"UISkin/%@/%@",APPSESSION.uistyle, imgfile]];
		if ([XRmenuFileManage fileExistByPath:ret2]) {
			return ret2;
		}else {
			return [self getGeneralImg:imgfile];
		}
	}
}

#pragma mark 菜品数据操作
// 去除imgNo中的符号-
+ (NSString *) convertImgNo:(NSString *)imgNo{
	return [[imgNo stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
}
//菜肴展示
+(NSString*) getDishGridImageFileName:(NSString*)dish_uuid Error:(XRmenuError**)outError
{
	NSString *filename = [NSString stringWithFormat:@"%@/grid/%@.png",kRootPathName, [self convertImgNo:dish_uuid]];
	NSString *ret = [self getDishImage:filename Error:outError];
    
	if ([ret isContainsString:@"XRmenu/default.png"]) {
        ret=[self getDishPhotoImageFileName:dish_uuid Error:outError];
	}
	return ret;
}
//大图展示
+(NSString*) getDishPhotoImageFileName:(NSString*)dish_uuid Error:(XRmenuError**)outError
{
	// todo 依据数据取指定目录
	NSString *filename = [NSString stringWithFormat:@"%@/dishphoto/%@.png",kRootPathName, [self convertImgNo:dish_uuid]];
	NSString *ret = [self getDishImage:filename Error:outError];
	
	if (*outError) {
		[*outError setValue:num(-20001) forKey:@"code"];//大图找不到
	}
	return ret; 
}

//大图展示
+(NSArray*) getDishPhotoDetailesImageFilesName:(NSString*)dish_uuid Error:(XRmenuError**)outError
{
	NSMutableArray* res =[[NSMutableArray alloc] initWithCapacity:1];
	NSString* docPath = [NSString stringWithFormat:@"%@/detail/%@",kRootPathName, [self convertImgNo:dish_uuid]];
	//NSString* docPath = [NSString stringWithFormat:@"%@/detail/%@",kRootPathName, dish_uuid];
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:docPath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:retResault]) {
		docPath = retResault;
	}else{
		NSString *ret2 = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:docPath];
		if ([[NSFileManager defaultManager] fileExistsAtPath:ret2]) {
			docPath = ret2;
		}else {
			docPath=nil;
		}
	} 
	if (!docPath) {
		return [ res autorelease];
	} 
	NSDirectoryEnumerator *dirEnumerater = [[NSFileManager defaultManager] enumeratorAtPath:docPath]; // 创建文件遍历管理类
	NSString *filePath;
	while((filePath = [dirEnumerater nextObject])) { // 循环获得文件名 
		if([filePath hasSuffix:@"png"] ) {
			[res addObject:[docPath stringByAppendingPathComponent: filePath]]; 
		} 
	} 
	return [res autorelease];  
}

+(NSArray*) getCommandPhotoImageFilesNameWithError:(XRmenuError**)outError
{	
	NSMutableArray* res =[[NSMutableArray alloc] initWithCapacity:1];
	NSString* docPath = @"commend";
	//NSString* docPath = [NSString stringWithFormat:@"%@/detail/%@",kRootPathName, dish_uuid];
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:docPath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:retResault]) {
		docPath = retResault;
	}else{
		NSString *ret2 = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:docPath];
		if ([[NSFileManager defaultManager] fileExistsAtPath:ret2]) {
			docPath = ret2;
		}else {
			docPath=nil;
		}
	} 
	if (!docPath) {
		return [ res autorelease];
	} 
	NSDirectoryEnumerator *dirEnumerater = [[NSFileManager defaultManager] enumeratorAtPath:docPath]; // 创建文件遍历管理类
	NSString *filePath;
	while((filePath = [dirEnumerater nextObject])) { // 循环获得文件名 
		if([filePath hasSuffix:@"png"] ) {
			[res addObject:[docPath stringByAppendingPathComponent: filePath]]; 
		} 
	} 
	return [res autorelease];  
	
}

//菜肴名称图片取得
+(NSString*) getDishNameImageFileName:(NSString*)dish_uuid Error:(XRmenuError**)outError
{ 
	// todo 依据数据取指定目录  还需增加一个开关控制是否使用名称贴图
	NSString *filename = [NSString stringWithFormat:@"%@/nameimage/%@/%@.png",kRootPathName, APPSESSION.language, [self convertImgNo:dish_uuid]];
	
	NSString *ret = [self getDishImage:filename Error:outError];
	
	if (outError) {
		[*outError setValue:num(-20003) forKey:@"code"];
	} 
	return ret;
}

+(NSString*)getDishImage:(NSString*)aPath Error:(XRmenuError**)outError
{
	NSString *ret = [self getGeneralImg:aPath];
    *outError=nil;
	if ([ret isEqualToString:@"No Image"]) {        
		NSString *errorMsg = [[NSString alloc] initWithFormat:@"%@(%@)",@"指定菜品的相关名字图片文件找不到", aPath];
		NSDictionary *uDict = [NSDictionary dictionaryWithObject:errorMsg forKey:NSLocalizedDescriptionKey];
		[errorMsg release];
		XRmenuError *error = [XRmenuError errorWithDomain:kErrorDomain code:-1 userInfo:uDict];
		if (error) {
			*outError = error;
		}
	}
	return ret;
}

+(NSString*) jsonfield:(NSString*)content
{
    content=[content stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
	NSDictionary *tmp = [content JSONValue];
	if (tmp) {
		if ([tmp objectForKey:APPSESSION.language]) {
			return [tmp objectForKey:APPSESSION.language];
		}else {
			return content;
		}
	}else {
		return content;
	}
}

+(NSInteger) getOrderedNum
{
	NSString* condition =[NSString stringWithFormat:@"rsToSaleOrder.sProductOrderId == '%@'", APPSESSION.currentOrderNo];
	NSPredicate *tmp = [NSPredicate predicateWithFormat:condition];
	NSMutableArray *orderlist = [XRmenuCoreData searchObjectsInContext:@"SaleOrderProduct" Predicate:tmp SortKey:@"sID" SortAscending:YES];
	return [orderlist count];
}

+(NSInteger) getProductNum:(Product*)item
{
	NSString *cond = [NSString stringWithFormat:@"rsToSaleOrder.sProductOrderId == '%@' AND rsToProduct.pProductId == '%@'", APPSESSION.currentOrderNo, item.pProductId];
	NSPredicate *tmp = [NSPredicate predicateWithFormat:cond];
	NSMutableArray *orderlist = [XRmenuCoreData searchObjectsInContext:@"SaleOrderProduct" Predicate:tmp SortKey:@"sID" SortAscending:YES];
	if ([orderlist count] > 0) {
		SaleOrderProduct *ret = (SaleOrderProduct*)[orderlist objectAtIndex:0];
		return [ret.sAmount intValue];
	}else {
		return 0;
	}

	return [orderlist count];
}

+(BOOL) insertNewOrder:(Product*)item
{
	XRmenuError *error = nil;
	[XRmenuCoreData insertNewOrderProduct:item Error:&error];
	if (error == nil) {
		return YES;
	}else {
		DebugLog(@"创建新单失败: %@", error);
		return NO;
	}
}
+(BOOL) updateProductNum:(Product*)item Num:(NSNumber*)n
{
	XRmenuError *error = nil;
	[XRmenuCoreData updateProductNum:item Increase:n Error:&error];
	if (error == nil) {
		return YES;
	}else {
		DebugLog(@"更新点单数据失败: %@", error);
		return NO;
	}
}

+(BOOL) clearProduct:(Product*)item
{
	XRmenuError *error = nil;
	[XRmenuCoreData clearProduct:item Error:&error];
	if (error == nil) {
		return YES;
	}else {
		DebugLog(@"删除点单数据失败: %@", error);
		return NO;
	}
}

+(BOOL) clearAllProduct
{
	XRmenuError *error = nil;
	[XRmenuCoreData clearAllSaleProduct:APPSESSION.currentOrderNo Error:&error];
	if (error == nil) {
		return YES;
	}else {
		DebugLog(@"清除点单数据失败: %@", error);
		return NO;
	}
}

+(NSMutableArray*) getAllCommendProduct
{
	NSString *cond = [NSString stringWithFormat:@"pIsRecommend == %@ && self.rsToCatalogs.rsToMenu.mMenuId == '%@'", num(1), APPSESSION.currentBookID];
	NSPredicate *pre = [NSPredicate predicateWithFormat:cond];
	return [XRmenuCoreData searchObjectsInContext:@"Product" Predicate:pre SortKey:@"pID" SortAscending:YES];
}
#pragma mark 大图风格下的数据操作
+(NSString*) haveAudioFile:(Product*)item Error:(XRmenuError**)outError
{
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"XRmenu/audio/%@_%@.mp3", [self convertImgNo:item.pEntityId],APPSESSION.language]];
	if (![XRmenuFileManage fileExistByPath:retResault]) {
		retResault = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"XRmenu/audio/%@_%@.mp3", [self convertImgNo:item.pEntityId],APPSESSION.language]];
		if (![XRmenuFileManage fileExistByPath:retResault]) {
			NSString *errorMsg = [[NSString alloc] initWithFormat:@"%@(%@)",@"指定菜品的相关音频文件找不到", retResault];
			NSDictionary *uDict = [NSDictionary dictionaryWithObject:errorMsg forKey:NSLocalizedDescriptionKey];
			[errorMsg release];
			XRmenuError *error = [XRmenuError errorWithDomain:kErrorDomain code:-20004 userInfo:uDict];
			if (error) {
				*outError = error;
			}
		}
	}
	return retResault;
}

+(NSString*) haveVedioFile:(Product*)item Error:(XRmenuError**)outError
{
	NSString *retResault = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"XRmenu/video/%@_%@.mp4", [self convertImgNo:item.pEntityId],APPSESSION.language]];
	if (![XRmenuFileManage fileExistByPath:retResault]) {
		retResault = [[XRmenuFileManage nativeDocumentPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"XRmenu/video/%@_%@.mp4", [self convertImgNo:item.pEntityId],APPSESSION.language]];
		if (![XRmenuFileManage fileExistByPath:retResault]) {
			NSString *errorMsg = [[NSString alloc] initWithFormat:@"%@(%@)",@"指定菜品的相关视频文件找不到", retResault];
			NSDictionary *uDict = [NSDictionary dictionaryWithObject:errorMsg forKey:NSLocalizedDescriptionKey];
			[errorMsg release];
			XRmenuError *error = [XRmenuError errorWithDomain:kErrorDomain code:-20005 userInfo:uDict];
			if (error) {
				*outError = error;
			}
		}
	}
	return retResault;
}

+(NSMutableArray*) haveRelationProduct:(Product*)item
{
	NSString *cond = [NSString stringWithFormat:@"pEntityId == '%@' AND pProductId != '%@'", item.pEntityId, item.pProductId];
	NSPredicate *rp = [NSPredicate predicateWithFormat:cond];
	return [XRmenuCoreData searchObjectsInContext:@"Product" Predicate:rp SortKey:@"pProductId" SortAscending:YES];
}

#pragma mark 数据刷新基本方法
+(NSMutableArray*) refreshProductByCatalogPath:(NSString*)cpath
{
	NSExpression *fieldname = [NSExpression expressionForKeyPath:@"rsToCatalogs.cGlobalPath"];
	NSExpression *cp = [NSExpression expressionForConstantValue:cpath];
	NSPredicate *pre = [NSComparisonPredicate predicateWithLeftExpression:fieldname rightExpression:cp modifier:NSDirectPredicateModifier type:NSBeginsWithPredicateOperatorType options:0];
	return [XRmenuCoreData searchObjectsInContext:@"Product" Predicate:pre SortKey:@"pID" SortAscending:YES];
}
+(NSMutableArray*) refreshProductByTag:(NSString*)tid
{
	NSString* str = [NSString stringWithFormat:@"self.rsToCatalogs.rsToMenu.mMenuId == '%@' && SUBQUERY(rsToProductTag, $x, $x.tTagId == '%@').@count >0",APPSESSION.currentBookID,tid];
	
	NSPredicate *pre = [NSPredicate predicateWithFormat:str];
	return [XRmenuCoreData searchObjectsInContext:@"Product" Predicate:pre SortKey:@"pID" SortAscending:YES];
}
@end
